<?php
/**
 * List all sub company associate to the current one as a list
 *
 * 
 */
use Hive\Company;
use Fox\Behaviors;
use Fox\Generator;
use Fox\Grid\FieldCat\Field\Renderer\Anchor;
use Fox\Html\Button;
$xview =  Xview::instance();
/* @var $contact Hive\Company */
$company = $xview->component('company', 'object');
$cpid = $company->getId();

// Customizing
$customizing = Hive\Company\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'company','act'=>'childrens','sac'=>null));

$cpid = $company->getId();
$header = new Fox\Html\Header\Content('childrens',Msg(Hive::BUNDLE,Hive::SUB_COMPANIES));

$subCompanies = $company->subCompanies();
if(count($subCompanies) == 0) {
	$html = '<p class="text-center alert">'.Msg(Hive::BUNDLE,Hive::THERE_ARE_NO_SUB_COMPANIES).'</p>';
} else {
	$grid = new Fox\Grid('gSubCompanies');
	$grid->fieldcat(array(array('company','company_name'),'nbcontacts'));
	$grid->fieldcat()->switchToObjectMode();
	$grid->fieldcat()->setFieldAttr('*', 'renderers',new Anchor(Generator::url(
		Company::BUNDLE,
		'main',
		null,
		array('cpid' => Generator::useAttribute('compid'))
	)));
	$grid->fieldcat()->setGenerators(array(
		'nbcontacts' => Generator::useFunction(function($data) {
			$nbContacts = $data->countContacts();
			return $nbContacts['all'] + $nbContacts['old'];
		})
	));
	$grid->fieldcat()->field('nbcontacts')->align('center');
	$grid->initialize($subCompanies);
	$html = $grid->display();
}
?>
<div id="messagesContent">
	<div><?php echo $header->render();?></div>
	<div class="contentArea">
		<div class="mbs gsl" id="contacts-container">
				<?php echo $html; ?>
		</div>
	</div>
</div>